<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>网页计算器</title>
</head>
<body>
    <p>
        数据1:<input type="text" id="data1">
    </p>
    <p>
        数据2:<input type="text" id="data2">
    </p>
    <p>
        操作:<button onclick="sum('add')">相加</button>
        <button onclick="sum('subtract')">相减</button>
        <button onclick="sum('take')">相乘</button>
        <button onclick="sum('divide')">相除</button>
    </p>
    <p>
        结果:<input type="text" id="result">
    </p>
    <script>
        function sum(a){
            var data1=document.querySelector("#data1").value;
            data1=Number(data1);
            var data2=document.querySelector("#data2").value;
            data2=Number(data2);
            switch (a){
                case "add":
                document.querySelector("#result").value=data1+data2;
                break;
                case "subtract":
                    document.querySelector("#result").value=data1-data2;
                    break;
                case "take":
                    document.querySelector("#result").value=data1*data2;
                    break;
                case "divide":
                    document.querySelector("#result").value=data1/data2;
                    break;
            }
        }
    </script>
</body>
</html>